<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,
        html {
            margin: 0 auto;
            padding: 0;
            width: 100%;
            height: 100%;
            background-color: lightseagreen;
            overflow: hidden;
        }

        * {
            box-sizing: border-box;
        }

        .layout {
            height: 100%;
            width: 100%;
            display: -webkit-flex;
            display: flex;
            flex-direction: row;
        }

        .layout-side {
            height: 100%;
            width: 100px;
            background-color: white;
        }

        .layout-main {
            height: 100%;
            background-color: lightblue;
            flex-grow: 1;
        }

        .img_each_container {
            margin-top: 1px;
            background-color: lightseagreen;
        }

        .debug_tools_container {
            width: 400px;
            height: 50px;
            line-height: 50px;
            padding: 0px 10px;
            background-color: lightslategray;
            position: absolute;
            bottom: 10px;
            left: 200px;
        }

        .debug_tools_container a {
            padding: 4px 10px;
            background-color: lightseagreen;
            color: white;
        }
    </style>
    <script src="https://cdn.bootcss.com/fabric.js/3.2.0/fabric.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>

    <div class="layout">

        <div class="layout-side">
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>



        </div>
        <div class="layout-main">
            <canvas id="myCanvas"></canvas>
        </div>

        <div class="debug_tools_container">
            <a href="###" id="getJson">获取JSON</a>
            <a href="###" id="loadJson">加载JSON</a>
        </div>


    </div>

    <script>
        function printf() {
            console.log(arguments);
        }

        var windowOption = {
            currentCanvas: {
                width: 0,
                height: 0
            }
        };

        window.onload = function () {








        }

        $(function () {

            // fabric.Object.prototype.setControlsVisibility({
            //     bl: true, // 左下
            //     br: true, // 右下
            //     mb: false, // 下中
            //     ml: false, // 中左
            //     mr: false, // 中右
            //     mt: false, // 上中
            //     tl: true, // 上左
            //     tr: true, // 上右
            //     mtr: true // 旋转控制键
            // })

            // rect.set({
            //     // 将此属性设定成 false 才有办法设定填满设定点
            //     transparentCorners: false,
            //     borderColor: 'red', // 边框顏色
            //     cornerColor: 'red', // 控制点填满色
            //     cornerSize: 10, // 控制点大小
            //     cornerStrokeColor: 'blue', // 控制点边框色
            // })


            windowOption.currentCanvas.width = $(".layout-main").width();
            windowOption.currentCanvas.height = $(".layout-main").height();

            printf(windowOption, "==========windowOption=========");

            var c = document.getElementById("myCanvas");
            c.width = windowOption.currentCanvas.width;
            c.height = windowOption.currentCanvas.height;

            var canvas = new fabric.Canvas('myCanvas');

            canvas.defaultCursor = 'crosshair'; //默认光标改成十字
            canvas.hoverCursor = 'pointer'; //悬浮光标改成手型



            canvas.width = windowOption.currentCanvas.width;
            canvas.height = windowOption.currentCanvas.height;

            var rect = new fabric.Rect({
                top: 50,
                left: 100,
                width: 100,
                height: 70,
                fill: 'red',

                padding: 1,
                borderDashArray: [5, 5],
                cornerStyle: 'circle'
            });

            canvas.add(rect);



            var imgElement = document.getElementById('img'); //声明我们的图片
            var imgInstance = new fabric.Image(imgElement, { //设置图片在canvas中的位置和样子
                left: 100,
                top: 100,
                width: 100,
                height: 100,
                angle: 30 //设置旋转
            });

            canvas.add(imgInstance); //加入到canvas中

            $(".layout-main").bind('mouseenter', function (e) {

                var imgUrl = $(e.relatedTarget).attr('src');

                var fromResData = {
                    dom: e.relatedTarget,
                    width: $(e.relatedTarget).width(),
                    height: $(e.relatedTarget).height(),
                }

                var img = new Image();
                img.onload = function () {
                    var imgInstance = new fabric.Image(imgElement, { //设置图片在canvas中的位置和样子
                        left: e.offsetX - fromResData.width / 2,
                        top: e.offsetY - fromResData.height / 2,
                        width: 100,
                        height: 100,
                        angle: 0 //设置旋转
                    });
                    canvas.add(imgInstance); //加入到canvas中
                }
                img.src = imgUrl;
            })


            const textbox = new fabric.Textbox('这是一段文字', {
                left: 50,
                top: 50,
                width: 150,
                fontSize: 20, // 字体大小
                fontWeight: 800, // 字体粗细
                fill: 'red', // 字体颜色
                fontStyle: 'italic', // 斜体
                fontFamily: 'Delicious', // 设置字体
                // stroke: 'green', // 描边颜色
                strokeWidth: 3, // 描边宽度
                hasControls: false,
                borderColor: 'orange',
                editingBorderColor: 'blue' // 点击文字进入编辑状态时的边框颜色
            });
            // 添加文字后，如下图
            canvas.add(textbox);



            $("#getJson").click(function () {
                // 导出当前画布信息
                const currState = canvas.toJSON(); // 导出的Json如下图

                console.log(currState, "=======currState==========");

                localStorage.setItem("currState", JSON.stringify(currState));
            });


            $("#loadJson").click(function () {
                var currState = localStorage.getItem("currState");

                // 加载画布信息
                canvas.loadFromJSON(currState, () => {
                    canvas.renderAll();
                });

            });

            var queue = {
                "version": "3.2.0",
                "objects": [{
                    "type": "rect",
                    "version": "3.2.0",
                    "originX": "left",
                    "originY": "top",
                    "left": 100,
                    "top": 50,
                    "width": 100,
                    "height": 70,
                    "fill": "red",
                    "stroke": null,
                    "strokeWidth": 1,
                    "strokeDashArray": null,
                    "strokeLineCap": "butt",
                    "strokeDashOffset": 0,
                    "strokeLineJoin": "miter",
                    "strokeMiterLimit": 4,
                    "scaleX": 1,
                    "scaleY": 1,
                    "angle": 0,
                    "flipX": false,
                    "flipY": false,
                    "opacity": 1,
                    "shadow": null,
                    "visible": true,
                    "clipTo": null,
                    "backgroundColor": "",
                    "fillRule": "nonzero",
                    "paintFirst": "fill",
                    "globalCompositeOperation": "source-over",
                    "transformMatrix": null,
                    "skewX": 0,
                    "skewY": 0,
                    "rx": 0,
                    "ry": 0
                }, {
                    "type": "image",
                    "version": "3.2.0",
                    "originX": "left",
                    "originY": "top",
                    "left": 100,
                    "top": 100,
                    "width": 100,
                    "height": 100,
                    "fill": "rgb(0,0,0)",
                    "stroke": null,
                    "strokeWidth": 0,
                    "strokeDashArray": null,
                    "strokeLineCap": "butt",
                    "strokeDashOffset": 0,
                    "strokeLineJoin": "miter",
                    "strokeMiterLimit": 4,
                    "scaleX": 1,
                    "scaleY": 1,
                    "angle": 0,
                    "flipX": false,
                    "flipY": false,
                    "opacity": 1,
                    "shadow": null,
                    "visible": true,
                    "clipTo": null,
                    "backgroundColor": "",
                    "fillRule": "nonzero",
                    "paintFirst": "fill",
                    "globalCompositeOperation": "source-over",
                    "transformMatrix": null,
                    "skewX": 0,
                    "skewY": 0,
                    "crossOrigin": "",
                    "cropX": 0,
                    "cropY": 0,
                    "src": "http://192.168.0.104:98/handerWritingWeb/test_html/%E4%B8%AD.png",
                    "filters": []
                }, {
                    "type": "image",
                    "version": "3.2.0",
                    "originX": "left",
                    "originY": "top",
                    "left": 93,
                    "top": 219,
                    "width": 100,
                    "height": 100,
                    "fill": "rgb(0,0,0)",
                    "stroke": null,
                    "strokeWidth": 0,
                    "strokeDashArray": null,
                    "strokeLineCap": "butt",
                    "strokeDashOffset": 0,
                    "strokeLineJoin": "miter",
                    "strokeMiterLimit": 4,
                    "scaleX": 1,
                    "scaleY": 1,
                    "angle": 0,
                    "flipX": false,
                    "flipY": false,
                    "opacity": 1,
                    "shadow": null,
                    "visible": true,
                    "clipTo": null,
                    "backgroundColor": "",
                    "fillRule": "nonzero",
                    "paintFirst": "fill",
                    "globalCompositeOperation": "source-over",
                    "transformMatrix": null,
                    "skewX": 0,
                    "skewY": 0,
                    "crossOrigin": "",
                    "cropX": 0,
                    "cropY": 0,
                    "src": "http://192.168.0.104:98/handerWritingWeb/test_html/%E4%B8%AD.png",
                    "filters": []
                }, {
                    "type": "image",
                    "version": "3.2.0",
                    "originX": "left",
                    "originY": "top",
                    "left": 243,
                    "top": 227,
                    "width": 100,
                    "height": 100,
                    "fill": "rgb(0,0,0)",
                    "stroke": null,
                    "strokeWidth": 0,
                    "strokeDashArray": null,
                    "strokeLineCap": "butt",
                    "strokeDashOffset": 0,
                    "strokeLineJoin": "miter",
                    "strokeMiterLimit": 4,
                    "scaleX": 1,
                    "scaleY": 1,
                    "angle": 0,
                    "flipX": false,
                    "flipY": false,
                    "opacity": 1,
                    "shadow": null,
                    "visible": true,
                    "clipTo": null,
                    "backgroundColor": "",
                    "fillRule": "nonzero",
                    "paintFirst": "fill",
                    "globalCompositeOperation": "source-over",
                    "transformMatrix": null,
                    "skewX": 0,
                    "skewY": 0,
                    "crossOrigin": "",
                    "cropX": 0,
                    "cropY": 0,
                    "src": "http://192.168.0.104:98/handerWritingWeb/test_html/%E4%B8%AD.png",
                    "filters": []
                }, {
                    "type": "image",
                    "version": "3.2.0",
                    "originX": "left",
                    "originY": "top",
                    "left": 103,
                    "top": 345,
                    "width": 100,
                    "height": 100,
                    "fill": "rgb(0,0,0)",
                    "stroke": null,
                    "strokeWidth": 0,
                    "strokeDashArray": null,
                    "strokeLineCap": "butt",
                    "strokeDashOffset": 0,
                    "strokeLineJoin": "miter",
                    "strokeMiterLimit": 4,
                    "scaleX": 1,
                    "scaleY": 1,
                    "angle": 0,
                    "flipX": false,
                    "flipY": false,
                    "opacity": 1,
                    "shadow": null,
                    "visible": true,
                    "clipTo": null,
                    "backgroundColor": "",
                    "fillRule": "nonzero",
                    "paintFirst": "fill",
                    "globalCompositeOperation": "source-over",
                    "transformMatrix": null,
                    "skewX": 0,
                    "skewY": 0,
                    "crossOrigin": "",
                    "cropX": 0,
                    "cropY": 0,
                    "src": "http://192.168.0.104:98/handerWritingWeb/test_html/%E4%B8%AD.png",
                    "filters": []
                }, {
                    "type": "image",
                    "version": "3.2.0",
                    "originX": "left",
                    "originY": "top",
                    "left": 237,
                    "top": 335,
                    "width": 100,
                    "height": 100,
                    "fill": "rgb(0,0,0)",
                    "stroke": null,
                    "strokeWidth": 0,
                    "strokeDashArray": null,
                    "strokeLineCap": "butt",
                    "strokeDashOffset": 0,
                    "strokeLineJoin": "miter",
                    "strokeMiterLimit": 4,
                    "scaleX": 1,
                    "scaleY": 1,
                    "angle": 0,
                    "flipX": false,
                    "flipY": false,
                    "opacity": 1,
                    "shadow": null,
                    "visible": true,
                    "clipTo": null,
                    "backgroundColor": "",
                    "fillRule": "nonzero",
                    "paintFirst": "fill",
                    "globalCompositeOperation": "source-over",
                    "transformMatrix": null,
                    "skewX": 0,
                    "skewY": 0,
                    "crossOrigin": "",
                    "cropX": 0,
                    "cropY": 0,
                    "src": "http://192.168.0.104:98/handerWritingWeb/test_html/%E4%B8%AD.png",
                    "filters": []
                }]
            }

            var myQueue = {
                "version": "3.2.0",
                "objects": []
            };


            // var toRenderStep = queue.objects.shift();

            // myQueue.objects.push(toRenderStep);

            // canvas.loadFromJSON(myQueue, () => {
            //     canvas.renderAll();
            // });

            
            canvas.on('object:modified', (e) => {
                console.log(e.target, "=========object:modified============") // e.target为当前编辑的Object
                // ...旋转，缩放，移动等编辑图层的操作都监听到
                // 所以如果有撤销/恢复的场景，这里可以保存编辑状态
            });



            // canvas.setZoom(1920 / 540);

            // var img = document.createElement("img");
            // document.body.append(img);
            // img.onload = function () {
            //     var imgInstance = new fabric.Image(img, {
            //         left: 0,
            //         top: 0,
            //         width: 100,
            //         height: 100
            //     });


            //     canvas.add(imgInstance)
            // }
            // img.crossOrigin = 'Anonymous';
            // img.src = '中.png';


            fabric.Image.fromURL('http://localhost:8881/hw/中.png', function (oImg) {

                oImg.width = 100;
                oImg.height = 100;
                oImg.top = 400;
                oImg.left = 600;

                canvas.add(oImg)
            });



        });
    </script>

</body>

</html>